package cn.tedu.qxhouserent.house.controller;

import cn.tedu.qxhouserent.house.pojo.param.AgentAddNewParam;
import cn.tedu.qxhouserent.house.pojo.param.AgentUpdateInfoParam;
import cn.tedu.qxhouserent.house.pojo.vo.AgentListItemVO;
import cn.tedu.qxhouserent.house.pojo.vo.AgentStandardVO;
import cn.tedu.qxhouserent.commons.pojo.vo.PageData;
import cn.tedu.qxhouserent.house.service.AgentService;
import cn.tedu.qxhouserent.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/agent")
@Api(tags = "经纪人管理模块")
@Slf4j
@Validated
public class AgentController {


    @Autowired
    AgentService agentService;

    @PostMapping("/add-new")
    @ApiOperation("添加经纪人")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/pms/agent/add-new')")
    public JsonResult addNew(@Valid AgentAddNewParam agentAddNewParam){
        log.debug("开始处理【添加经纪人】业务，参数：{}",agentAddNewParam);
        agentService.addNew(agentAddNewParam);
        log.debug("处理【添加经纪人】完毕");
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("根据ID删除经纪人")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "经纪人ID", required = true, dataType = "long")
    })
    @PreAuthorize("hasAuthority('/pms/agent/delete')")
    public JsonResult deleteById(@Range(min = 1, message = "请提交合法的ID值！")
                                 @RequestParam Long id) {
        log.debug("开始处理【根据ID删除经纪人】的请求，参数：{}", id);
        agentService.deleteById(id);
        log.debug("处理【根据ID删除经纪人】完毕！");
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改经纪人详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "经纪人ID", required = true, dataType = "long")
    })
    @PreAuthorize("hasAuthority('/pms/agent/update')")
    public JsonResult updateById(@RequestParam @Range(min = 1, message = "请提交有效的ID值！") Long id,
                                 @Valid AgentUpdateInfoParam agentUpdateInfoParam) {
        log.debug("开始处理【修改经纪人】的业务，参数：{}", agentUpdateInfoParam);
        agentService.updateById(id, agentUpdateInfoParam);
        log.debug("处理【修改经纪人】完毕");
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用经纪人")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "经纪人ID", required = true, dataType = "long")
    })
    @PreAuthorize("hasAuthority('/pms/agent/update')")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用经纪人】的请求，参数：{}", id);
        agentService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用经纪人")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "经纪人ID", required = true, dataType = "long")
    })
    @PreAuthorize("hasAuthority('/pms/agent/update')")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用经纪人】的请求，参数：{}", id);
        agentService.setDisable(id);
        return JsonResult.ok();
    }

    @GetMapping("/standard")
    @ApiOperation("根据ID查询经纪人")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "经纪人ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询经纪人】的请求，参数：{}", id);
        AgentStandardVO result = agentService.getStandardById(id);
        log.debug("处理【根据ID查询经纪人】完毕");
        return JsonResult.ok(result);
    }

    @GetMapping("/list")
    @ApiOperation("查询经纪人列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "query", value = "查询类型", paramType = "query", dataType = "string")
    })
    public JsonResult list(@Range(min = 1, message = "请提供正确的页码值！") Integer page, String query) {
        log.debug("开始处理【查询经纪人列表】的请求，页码：{}", page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<AgentListItemVO> pageData = null;
        if ("all".equals(query)) {
            pageData = agentService.list(1, Integer.MAX_VALUE);
        } else {
            pageData = agentService.list(page);
        }
        log.debug("处理【查询经纪人列表】完毕");
        return JsonResult.ok(pageData);
    }
}
